//
//  SNAccountView.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/21.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNAccountSectionHeaderView.h"
#import "SNAccountView.h"
#import "SNAccountCell.h"
@interface SNAccountView()<UICollectionViewDataSource,UICollectionViewDelegate>
/** UICollectionView * collectionView */
@property (weak, nonatomic) UICollectionView * collectionView;
/** models */
@property (nonatomic,strong) NSArray<NSDictionary *> * models;

/** 客服提供的模型 */
@property (nonatomic,strong) NSArray<NSDictionary *> * customerServiceModels;

@end
@implementation SNAccountView

- (NSArray<NSDictionary *> *)models {
    if (_models == nil) {
        
        
        NSString * filePath = [[NSBundle mainBundle] pathForResource:@"accountlistTEST.plist" ofType:nil];
        NSArray * array = [NSArray arrayWithContentsOfFile:filePath];
        
        _models = array;
    }
    return _models;
}
- (NSArray<NSDictionary *> *)customerServiceModels {
    if (_customerServiceModels == nil) {
        
        
        //        NSString * filePath = [[NSBundle mainBundle] pathForResource:@"accountlistTEST11.plist" ofType:nil];
        NSString * filePath = [[NSBundle mainBundle] pathForResource:@"accountlistZSHJ.plist" ofType:nil];
        
        NSArray * array = [NSArray arrayWithContentsOfFile:filePath];
        
        _customerServiceModels = array;
    }
    return _customerServiceModels;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addViews:frame];
        [self setupUI];
        
    }
    return self;
}
- (void)addViews:(CGRect)frame
{
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    /** 垂直方向 间距 */
    flowLayout.minimumLineSpacing = 5;
    /** 横向方向 间距 */
    flowLayout.minimumInteritemSpacing = 5;
    /** 内缩 内容 */
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
    NSInteger colNum = 3;
    
    CGFloat w =  ([UIScreen mainScreen].bounds.size.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right - flowLayout.minimumInteritemSpacing * (colNum - 1)) / colNum;
    
    /** item大小 */
    flowLayout.itemSize = CGSizeMake(w,69);
    /** headerView大小 */
    flowLayout.headerReferenceSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 44);
    //    /** footerView大小 */
    //    flowLayout.footerReferenceSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 69);
    /** 滚动方向 */
    // flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
    
    self.collectionView = collectionView;
    /** 分页处理 */
    // collectionView.pagingEnabled = YES;
    
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    [collectionView registerNib:[UINib nibWithNibName:@"SNAccountCell" bundle:nil] forCellWithReuseIdentifier:@"SNAccountCell"];
    [self addSubview:collectionView];
    [collectionView registerClass:[SNAccountSectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SNAccountSectionHeaderView"];
    //    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionReusableViewFooter"];
}
- (void)setupUI
{
    
}
#pragma mark - <UICollectionViewDataSource>
/** section 数量 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
/** item 数量 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (section == 0) {
        return self.customerServiceModels.count;
    }else{
        
        return self.models.count;
    }
}
/** UICollectionViewCell */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SNAccountCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SNAccountCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    if (indexPath.section == 0) {
        cell.model = self.customerServiceModels[indexPath.row];
    }else{
        
        cell.model = self.models[indexPath.row];
    }
    
    return cell;
}
/** UICollectionReusableView */
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if (kind == UICollectionElementKindSectionHeader) {
        //此处是headerView
        SNAccountSectionHeaderView *  headerReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"SNAccountSectionHeaderView" forIndexPath:indexPath];
        headerReusableView.backgroundColor = [UIColor whiteColor];
        //        headerReusableView.layer.borderColor = [UIColor blackColor].CGColor;
        //        headerReusableView.layer.borderWidth = 1.0;
        if (indexPath.section == 0) {
            //            headerReusableView.titleLabel.text = @"客服提供的账号密码";
            headerReusableView.titleLabel.text = @"生产环境的账号密码";
            
        }else{
            headerReusableView.titleLabel.text = @"自己收集的账号密码...";
        }
        return headerReusableView;
    }else if(kind == UICollectionElementKindSectionFooter){
        //此处是footerView
        UICollectionReusableView *  footerReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusableViewFooter" forIndexPath:indexPath];
        footerReusableView.backgroundColor = [UIColor greenColor];
        footerReusableView.layer.borderColor = [UIColor blackColor].CGColor;
        footerReusableView.layer.borderWidth = 1.0;
        return footerReusableView;
    }else{
        return nil;
    }
}
#pragma mark - <UICollectionViewDelegate>
// 选中操作
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //    NSLog(@"%@ | %s",self.class,__func__);
    
    SNAccountCell * cell = (SNAccountCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor orangeColor];
    
    
    NSDictionary * infoDict = nil;
    
    if (indexPath.section == 0) {
        infoDict = self.customerServiceModels[indexPath.row];
    }else{
        infoDict = self.models[indexPath.row];
    }
    
    
    !self.tapActionBlock ?: self.tapActionBlock(infoDict);
}
// 取消选中操作
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@ | %s",self.class,__func__);
    
    SNAccountCell * cell = (SNAccountCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
}
@end
